//
//  CellForSettingVC.m
//  echo
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "CellForSettingVC.h"
#import <DKNightVersion/DKNightVersion.h>

@interface CellForSettingVC ()

@end
@implementation CellForSettingVC

- (void)dealloc{
    [_backView release];
    [_imageViewForLeft release];
    [_labelForContent release];
    [_swichForNightModel release];
    [_labelForCache release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backView = [[UIView alloc]init];
        [self.contentView addSubview:self.backView];
        self.backView.backgroundColor = [UIColor whiteColor];
        [self.backView release];
        
        self.imageViewForLeft = [[UIImageView alloc]init];
        [self.backView addSubview:self.imageViewForLeft];
        [self.imageViewForLeft release];
        
        self.labelForContent = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForContent];
        [self.labelForContent release];
        self.labelForContent.textAlignment = 0;
        
        self.swichForNightModel = [[UISwitch alloc]init];
        [self.backView addSubview:self.swichForNightModel];
        [self.swichForNightModel release];
        self.swichForNightModel.hidden = YES;
        [self.swichForNightModel addTarget:self action:@selector(handleNightModel) forControlEvents:UIControlEventValueChanged];
        
        [NSUserDefaults standardUserDefaults];
        
        /** 判断开关状态 */
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
            
            self.swichForNightModel.on = YES;
        } else {
            
            self.swichForNightModel.on = NO;
        }
        
        
        self.labelForCache = [[UILabel alloc]init];
        [self.backView addSubview:self.labelForCache];
        [self.labelForCache release];
        self.labelForCache.textAlignment = 1;
        self.labelForCache.hidden = YES;
        
        [self becomeNightModel];
    }
    return self;
}

- (void)becomeNightModel{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    self.backView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
}

- (void)handleNightModel{
    if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) {
        
        [DKNightVersionManager dawnComing];
        
        [[NSUserDefaults standardUserDefaults] setBool:self.swichForNightModel.on forKey:@"夜间模式"];
        
    } else {
        
        [DKNightVersionManager nightFalling];
        
        [[NSUserDefaults standardUserDefaults] setBool:self.swichForNightModel.on forKey:@"夜间模式"];
        
    }
    
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.backView.frame = self.contentView.frame;
    self.imageViewForLeft.frame = CGRectMake(15 * WIDTH, 5 * HEIGHT, 35 * WIDTH, 35 * WIDTH);
    self.labelForContent.frame = CGRectMake(70 * WIDTH, 5 * HEIGHT, 200 * WIDTH, 40 * HEIGHT);
    self.swichForNightModel.frame = CGRectMake(340 * WIDTH, 10 * HEIGHT, 0, 0);
    self.labelForCache.frame = CGRectMake(310 * WIDTH, 8 * HEIGHT, 90 * WIDTH, 35 * HEIGHT);
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
